package com.anda.logistics.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

public class QueryFilter {
	private long [] ids;
	private Map<String, String> map;
	private List<SqlFilter> list;
	
	public QueryFilter(){}
	public QueryFilter(long[] params){
		this.ids = params;
	}
	public long[] getIds() {
		return ids;
	}
	public void setIds(long[] ids) {
		this.ids = ids;
	}
	@Override
	public String toString() {
		return "QueryFilter [ids=" + Arrays.toString(ids) + ", map=" + map
				+ ", list=" + list + "]";
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	public List<SqlFilter> getList() {
		return list;
	}
	public void setList(List<SqlFilter> list) {
		this.list = list;
	}

	public Query setSqlFilterValues(Query query){
		if (list != null){
			for (SqlFilter sf: list ){
				switch(sf.getType()){
				case SqlFilter.DOUBLE:
					query.setDouble(sf.getCol(), Double.parseDouble(sf.getVal()));
					break;
				case SqlFilter.FLOAT:
					query.setFloat(sf.getCol(), Float.parseFloat(sf.getVal()));
					break;
				case SqlFilter.INT:
					query.setInteger(sf.getCol(),Integer.parseInt(sf.getVal()));
					break;
				case SqlFilter.LONG:
					query.setLong(sf.getCol(),Long.parseLong(sf.getVal()));
					break;
				case SqlFilter.STRING:
					query.setString(sf.getCol(),sf.getVal());
					break;
				}
			}
		}
		return query;
	}
	
	public static class SqlFilter {
		String sql;
		String col;
		int type;
		String val;
		boolean pre;
		
		public static final int LONG = 1;
		public static final int INT = 2;
		public static final int FLOAT = 3;
		public static final int DOUBLE = 4;
		public static final int STRING = 5;
		public SqlFilter(){}
		public SqlFilter(String sql, String col, int type, String val, boolean pre){
			this.sql = sql;
			this.col = col;
			this.type = type;
			this.val = val;
			this.pre = pre;
		}
		@Override
		public String toString() {
			return "SqlFilter [sql=" + sql + ", col=" + col + ", type=" + type
					+ ", val=" + val + "]";
		}
		public String getSql() {
			return sql;
		}
		public void setSql(String sql) {
			this.sql = sql;
		}
		public String getCol() {
			return col;
		}
		public void setCol(String col) {
			this.col = col;
		}
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public String getVal() {
			return val;
		}
		public void setVal(String val) {
			this.val = val;
		}
		public boolean isPre() {
			return pre;
		}
		public void setPre(boolean pre) {
			this.pre = pre;
		}
	}
}
